﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bzzb.Model;
using FreeSql.Internal.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Bzzb.Controllers
{
    public class JiShiXiangController : BaseController
    {
        private readonly ILogger<JiShiXiangController> _logger;
        public JiShiXiangController(ILogger<JiShiXiangController> logger, IFreeSql fsql) : base(fsql)
        {
            this._logger = logger;
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Index([FromForm] string content, [FromForm] int doPage = 1, [FromForm] int pageIndex = 1, [FromForm] int pageSize = 10)
        {
            DynamicFilterInfo dyfilter = JsonConvert.DeserializeObject<DynamicFilterInfo>(@"
{
  'Logic' : 'And',
  'Filters' :
  [
    {
      'Field' : 'name',
      'Operator' : 'Contains',
      'Value' : '" + content + @"',
    }
  ]
}
");
            List<JiShiXiang> jishixiangs;
            long total = 0;
            if (doPage == 0)
            {
                jishixiangs = await _fsql.Select<JiShiXiang>().WhereDynamicFilter(dyfilter).ToListAsync();
            }
            else {
                jishixiangs = await _fsql.Select<JiShiXiang>().WhereDynamicFilter(dyfilter).Count(out total).Page(pageIndex, pageSize).ToListAsync();
            }
            
            _logger.LogInformation(_fsql.Select<JiShiXiang>().WhereDynamicFilter(dyfilter).Page(pageIndex, pageSize).ToSql());
            JObject data = new();
            data.Add("total", total);
            data.Add("list", JArray.FromObject(jishixiangs));
            return RenderJson(data: data);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Save([FromForm] JiShiXiang jishixiang)
        {
            long id = await _fsql.Insert(jishixiang).ExecuteIdentityAsync();
            jishixiang.Id = id;

            return RenderJson(data: jishixiang);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Delete([FromForm] long id) {
            int affrows = 0;
            _fsql.Transaction(() => {
                _fsql.Delete<JiShiXiang>().Where(jsx => jsx.Id == id).ExecuteAffrows();

                _fsql.Delete<DingQiKongZhiXiang>().Where(dqkzx => dqkzx.Jsxid == id).ExecuteAffrows();
            });
            return RenderJson(data: affrows);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Update([FromForm] JiShiXiang jishixiang) {
            int affrows = await _fsql.Update<JiShiXiang>().SetSourceIgnore(jishixiang, colval => colval == null).ExecuteAffrowsAsync();
            return RenderJson(data: affrows);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Show([FromForm] long id) {
            JiShiXiang jishixiang = await _fsql.Select<JiShiXiang>().Where(jsx => jsx.Id == id).ToOneAsync();
            return RenderJson(data: jishixiang);
        }
    }
}
